package com.kl.controller;

import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.User;
import com.kl.mapper.UserMapper;
import com.kl.service.IUserService;
import com.kl.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
@Api(value = "前台登录注册功能接口")
public class CodeController {

    /*@Autowired
    private RocketMQTemplate rocketMQTemplate;*/
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "注册手机号发送服务")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone",value = "手机号",required = true,paramType = "query")
    })
    @PostMapping("code")
    public Result code(String phone){
        //rocketMQTemplate.convertAndSend("phone-topic",phone);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation(value = "前台注册功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name="password",value = "密码",required = true,paramType = "query"),
            @ApiImplicitParam(name="phone",value = "手机号",required = true,paramType = "query"),
            @ApiImplicitParam(name="code",value = "验证码",required = true,paramType = "query")
    })
    @PostMapping("register")
    public Result register(User user, @RequestParam("code") String code){
        String code1 = redisTemplate.opsForValue().get(user.getPhone()).toString();
        System.out.println(code1);
        if (!code.equals(code1)){
            return Result.build(ConstantEnum.ERROR);
        }
        String salt = ShiroUtils.generateSalt(6);
        user.setSale(salt);
        String md5 = ShiroUtils.encryptPassword("MD5", user.getPassword(), salt, 3);
        user.setPassword(md5);
        user.setStatus(1);
        boolean b=user.insert();
//        System.out.println(user.getId()+"111111");
        if (b){
            return Result.build(ConstantEnum.SUCCESS);
        }
        boolean insert = user.insert();
        System.out.println(user.getId());
        return Result.build(ConstantEnum.ERROR);
    }



}
